package 国际象棋0710;

import java.awt.*;

public class Tool implements Data{
    public Chess[][] initChessList(){
        Chess[][] chessList = new Chess[ROW][COL];
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COL; j++) {
                chessList[i][j] = new Chess();
            }
        }

        chessList[0][0] = new Chess(1, "车");
        chessList[0][7] = new Chess(1, "车");
        chessList[7][0] = new Chess(2, "车");
        chessList[7][7] = new Chess(2, "车");

        chessList[0][1] = new Chess(1, "马");
        chessList[0][6] = new Chess(1, "马");
        chessList[7][1] = new Chess(2, "马");
        chessList[7][6] = new Chess(2, "马");

        chessList[0][2] = new Chess(1, "象");
        chessList[0][5] = new Chess(1, "象");
        chessList[7][2] = new Chess(2, "象");
        chessList[7][5] = new Chess(2, "象");

        chessList[0][3] = new Chess(1, "后");
        chessList[0][4] = new Chess(1, "王");
        chessList[7][3] = new Chess(2, "后");
        chessList[7][4] = new Chess(2, "王");

        for (int i = 0; i < 8; i++) {
            chessList[1][i] = new Chess(1, "兵");
        }

        for (int i = 0; i < 8; i++) {
            chessList[6][i] = new Chess(2, "兵");
        }
        return chessList;
    }

    public String[] getGames(){
        int num = getGamesNum();
        String[] games = new String[0];
        return games;
    }

    public int getGamesNum(){
        int num = 0;
        return num;
    }
    //画棋盘
    public void drawChessPad(Graphics g){
        g.setColor(Color.black);
        g.drawRect(X - 1,Y - 1,SIZE * COL + 1 , SIZE * ROW + 1);
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COL; j++) {
                g.setColor((i + j) % 2 == 0? newWhite : newBlack);
                g.fillRect(X + j * SIZE, Y + i * SIZE, SIZE, SIZE);
            }
        }
    }
    //画棋子
    public void initChesses(Chess[][] chessList, Graphics g){
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COL; j++) {
                chessList[i][j] = new Chess();
            }
        }

        chessList[0][0] = new Chess(1, "车");
        chessList[0][7] = new Chess(1, "车");
        chessList[7][0] = new Chess(2, "车");
        chessList[7][7] = new Chess(2, "车");

        chessList[0][1] = new Chess(1, "马");
        chessList[0][6] = new Chess(1, "马");
        chessList[7][1] = new Chess(2, "马");
        chessList[7][6] = new Chess(2, "马");

        chessList[0][2] = new Chess(1, "象");
        chessList[0][5] = new Chess(1, "象");
        chessList[7][2] = new Chess(2, "象");
        chessList[7][5] = new Chess(2, "象");

        chessList[0][3] = new Chess(1, "后");
        chessList[0][4] = new Chess(1, "王");
        chessList[7][3] = new Chess(2, "后");
        chessList[7][4] = new Chess(2, "王");

        for (int i = 0; i < 8; i++) {
            chessList[1][i] = new Chess(1, "兵");
        }

        for (int i = 0; i < 8; i++) {
            chessList[6][i] = new Chess(2, "兵");
        }
        drawChessList(chessList, g);
    }

    public void drawChessList(Chess[][] chessList, Graphics g){
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COL; j++) {
                drawChess(i, j, chessList[i][j], g);
            }
        }
    }

    public void drawChess(int r, int c, Chess chess, Graphics g){
        if (chess.chessFlag == 0){
            if ((r + c) % 2 == 1){
                g.setColor(newBlack);
                g.fillRect(X + c * SIZE, Y + r * SIZE, SIZE, SIZE);
            }else if ((r + c) % 2 == 0){
                g.setColor(newWhite);
                g.fillRect(X + c * SIZE, Y + r * SIZE, SIZE, SIZE);
            }
            return;
        }
        if (chess.chessFlag == 1){
            g.setColor(Color.black);
        } else if (chess.chessFlag == 2) {
            g.setColor(Color.white);
        }
        g.setFont(FONT);
        g.drawString(chess.kind, panelX(c), panelY(r));
    }

    public int calChessCOL(int x){
        int c = (x - X) / SIZE;
        return c;
    }

    public int calChessROW(int y){
        int r = (y - Y) / SIZE;
        return r;
    }

    public int panelX(int c){
        return X + c * SIZE + 15;
    }

    public int panelY(int r){
        return Y + r * SIZE + 40;

    }
    //能否落子
    public Boolean isFall(int preR, int preC, int nowR, int nowC, Chess[][] chessList){
        switch (chessList[preR][preC].kind){
            case "车" ->{
                return ruleOfRook( preR,  preC,  nowR,  nowC, chessList);
            }
            case "马" ->{
                return ruleOfKnight( preR,  preC,  nowR,  nowC, chessList);
            }
            case "象" ->{
                return ruleOfBishop( preR,  preC,  nowR,  nowC, chessList);
            }
            case "后" ->{
                return ruleOfQueen( preR,  preC,  nowR,  nowC, chessList);
            }
            case "王" ->{
                return ruleOfKing( preR,  preC,  nowR,  nowC, chessList);
            }
            case "兵" ->{
                return ruleOfPawn( preR,  preC,  nowR,  nowC, chessList);
            }
        }
        return false;
    }
    //车
    public Boolean ruleOfRook(int preR, int preC, int nowR, int nowC, Chess[][] chessList){
        if (isRow(preR, nowR) || isCol(preC, nowC)){
            if (!pathIsEmpty( preR,  preC,  nowR,  nowC, chessList)){
                if (chessList[nowR][nowC].chessFlag == 0){ //移动
                    return true;
                }else if (chessList[preR][preC].chessFlag != chessList[nowR][nowC].chessFlag){ //吃棋子
                    return true;
                }
            }
        }
        return false;
    }
    //马
    public Boolean ruleOfKnight(int preR, int preC, int nowR, int nowC, Chess[][] chessList){
        if (isSun( preR,  preC,  nowR,  nowC)){
            if (chessList[nowR][nowC].chessFlag == 0){ //移动
                return true;
            } else if (chessList[preR][preC].chessFlag != chessList[nowR][nowC].chessFlag) { //吃
                return true;
            }
        }
        return false;
    }
    //象
    public Boolean ruleOfBishop(int preR, int preC, int nowR, int nowC, Chess[][] chessList){
        if (isLUtoRD( preR,  preC,  nowR,  nowC) || isRUtoLD( preR,  preC,  nowR,  nowC)){
            if (!pathIsEmpty( preR,  preC,  nowR,  nowC, chessList)){
                if (chessList[nowR][nowC].chessFlag == 0){ //移动
                    return true;
                } else if (chessList[preR][preC].chessFlag != chessList[nowR][nowC].chessFlag) { //吃
                    return true;
                }
            }
        }
        return false;
    }
    //后
    public Boolean ruleOfQueen(int preR, int preC, int nowR, int nowC, Chess[][] chessList){
        if (isRow(preR, nowR) || isCol(preC, nowC) || isLUtoRD( preR,  preC,  nowR,  nowC) || isRUtoLD( preR,  preC,  nowR,  nowC)){
            if (!pathIsEmpty( preR,  preC,  nowR,  nowC, chessList)){
                if (chessList[nowR][nowC].chessFlag == 0){ //移动
                    return true;
                } else if (chessList[preR][preC].chessFlag != chessList[nowR][nowC].chessFlag) { //吃
                    return true;
                }
            }
        }
        return false;
    }
    //王
    public Boolean ruleOfKing(int preR, int preC, int nowR, int nowC, Chess[][] chessList){
        if (Math.abs(preR - nowR) <= 1 && Math.abs(preC - nowC) <= 1){
            if (chessList[nowR][nowC].chessFlag == 0){//移动
                return true;
            } else if (chessList[nowR][nowC].chessFlag != chessList[preR][preC].chessFlag) { //吃
                return true;
            }
        }
        return false;
    }
    //兵
    public Boolean ruleOfPawn(int preR, int preC, int nowR, int nowC, Chess[][] chessList){
        if (isCol(preC, nowC)){
            if (Math.abs(nowR - preR) == 2 && isPawnFM( preR, chessList[preR][preC])){
                if (!pathIsEmpty( preR,  preC,  nowR,  nowC, chessList)){
                    if (chessList[nowR][nowC].chessFlag == 0){ //移动
                        return true;
                    }
                }
            } else if (Math.abs(nowR - preR) == 1) {
                if (chessList[nowR][nowC].chessFlag != chessList[preR][preC].chessFlag){ //移动
                    return true;
                }
            }
        } else if (isLUtoRD( preR,  preC,  nowR,  nowC) || isRUtoLD( preR,  preC,  nowR,  nowC)) {
            if (chessList[nowR][nowC].chessFlag != chessList[preR][preC].chessFlag){ //吃
                return true;
            }
        }
        return false;
    }

    //判断两个格子中间是否有棋子
    public Boolean pathIsEmpty(int preR, int preC, int nowR, int nowC, Chess[][] chessList){
        if (isRow(preR, nowR)){ //横走
            if (preC < nowC){
                for (int i = preC + 1; i < nowC; i++) {
                    if (chessList[preR][i].chessFlag != 0){
                        return true;
                    }
                }
            }else {
                for (int i = preC - 1; i > nowC; i--) {
                    if (chessList[preR][i].chessFlag != 0){
                        return true;
                    }
                }
            }
        }else if (isCol(preC, nowC)){ //竖走
            if (preR < nowR){
                for (int i = preR + 1; i < nowR; i++) {
                    if (chessList[i][preC].chessFlag != 0){
                        return true;
                    }
                }
            }else {
                for (int i = preR - 1; i > nowR; i--) {
                    if (chessList[i][preC].chessFlag != 0){
                        return true;
                    }
                }
            }
        } else if (isLUtoRD( preR,  preC,  nowR,  nowC)) { //左上右下
            if (preC < nowC){
                int k = 1;
                for (int i = preC + 1; i < nowC; i++) {
                    int j = preR + k;
                    if (chessList[j][i].chessFlag != 0){
                        return true;
                    }
                    k++;
                }
            }else {
                int k = 1;
                for (int i = preC - 1; i > nowC; i--) {
                    int j = preR - k;
                    if (chessList[j][i].chessFlag != 0){
                        return true;
                    }
                    k++;
                }
            }
        } else if (isRUtoLD( preR,  preC,  nowR,  nowC)) { //右上左下
            if (preC < nowC){
                int k = 1;
                for (int i = preC + 1; i < nowC; i++) {
                    int j = preR - k;
                    if (chessList[j][i].chessFlag != 0){
                        return true;
                    }
                    k++;
                }
            }else {
                int k = 1;
                for (int i = preC - 1; i > nowC; i--) {
                    int j = preR + k;
                    if (chessList[j][i].chessFlag != 0){
                        return true;
                    }
                    k++;
                }
            }
        }
        return false;
    }

    public Boolean isRow(int preR, int nowR){
        return preR == nowR;
    }

    public Boolean isCol(int preC, int nowC){
        return preC == nowC;
    }

    public Boolean isLUtoRD(int preR, int preC, int nowR, int nowC){
        return preR - nowR == preC - nowC;
    }

    public Boolean isRUtoLD(int preR, int preC, int nowR, int nowC){
        return preR - nowR == nowC - preC;
    }

    public Boolean isSun(int preR, int preC, int nowR, int nowC){
        int r = Math.abs(nowR - preR) ;
        int c = Math.abs(nowC - preC) ;
        if (r == 2){
            return c == 1 ;
        }
        if (r == 1){
            return c == 2 ;
        }
        return false;
    }

    public Boolean isPawnFM(int preR, Chess pre){
        return pre.chessFlag == 1 && preR == 1 || pre.chessFlag == 2 && preR == 6;
    }

    //棋子移动
    public void move(int preR, int preC, int nowR, int nowC, Chess[][] chessList, Graphics g){
        chessList[nowR][nowC].chessFlag = chessList[preR][preC].chessFlag;
        chessList[nowR][nowC].kind = chessList[preR][preC].kind;
        chessList[preR][preC] = new Chess();
        drawChess(preR, preC, chessList[preR][preC], g);
        drawChess(nowR, nowC, chessList[nowR][nowC], g);
    }

    //吃棋子
//    public void eat(Chess pre, Chess now, Chess[][] chessList){
//        chessList[now.r][now.c].chessFlag = chessList[pre.r][pre.c].chessFlag;
//        chessList[now.r][now.c].kind = chessList[pre.r][pre.c].kind;
//        chessList[pre.r][now.c] = new Chess(pre.r, pre.c);
//
//    }

    //无效操作
    public Boolean uselessAct(int preR, int preC, int nowR, int nowC, Chess[][] chessList, Chess choice){

        if (choice == null && chessList[nowR][nowC].kind == "空"){
            System.out.println("-------3");
            return false;
        }
        if (preR == nowR && preC == nowC){
            System.out.println("-------6");
            return false;
        }
        if (choice != null && chessList[nowR][nowC].chessFlag == choice.chessFlag) {
            System.out.println("-------9");
            return false;
        }

        return true;
    }

}
